clear
set more off

local path0="C:\Dropbox\GeneticsProject (1)\REStat\round_accepted_replication_files\"

cd "`path0'Data\Spolaore\"

*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
insheet using "Countrycodes.txt", names clear
save "Countrycodes1.dta", replace

insheet using "Countrycodes.txt", names clear
rename country_1 country_2
rename countrycode countrycode2
save "Countrycodes2.dta", replace

*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*** append ISO 3-letter country codes to the Spolaore-Wacziarg data

use "`path0'Data\Spolaore\genetic_distance.dta", clear

joinby country_1 using Countrycodes1.dta, unmatched(master)
tab _merge
drop _merge

joinby country_2 using Countrycodes2.dta, unmatched(master)
tab _merge
drop _merge

drop if country_2=="Montserrat"

drop if countrycode==""
drop if countrycode2==""
drop  wacziarg_1 wacziarg_2
drop country

compress
save temp000, replace

*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*** create distance from the UK
use temp000, clear

keep if countrycode2=="GBR" | countrycode=="GBR"


local new = _N + 1
set obs `new'

egen tt=fill(1(1)10)

gen     countrycode0=countrycode if countrycode2=="GBR"
replace countrycode0=countrycode2 if countrycode=="GBR" & countrycode0==""
replace countrycode0="GBR" if tt==_N

foreach var in fst_distance_dominant nei_distance_dominant fst_distance_weighted nei_distance_weighted fst_stderr_dominant ///
			   nei_stderr_dominant fst_stderr_weighted nei_stderr_weighted gendist_fst_1500 gendist_nei_1500 fst_stderr_1500 nei_stderr_1500 {
		replace `var'=0 if tt==_N
}

foreach var in fst_stderr_dominant nei_stderr_dominant fst_stderr_weighted nei_stderr_weighted fst_stderr_1500 nei_stderr_1500 {
		replace `var'=1 if tt==_N
}


foreach var in fst_distance_dominant nei_distance_dominant fst_distance_weighted nei_distance_weighted fst_stderr_dominant ///
			   nei_stderr_dominant fst_stderr_weighted nei_stderr_weighted gendist_fst_1500 gendist_nei_1500 fst_stderr_1500 nei_stderr_1500 {
		rename `var' SP_UK_`var'
}

keep SP_UK* countrycode0
rename countrycode0 countrycode

keep countrycode SP_UK_fst_distance_dominant SP_UK_nei_distance_dominant


compress
save "`path0'Workfiles\distance_SP_UK.dta", replace


*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*** create distance from the UK
use temp000, clear

keep if countrycode2=="USA" | countrycode=="USA"


local new = _N + 1
set obs `new'

egen tt=fill(1(1)10)

gen     countrycode0=countrycode if countrycode2=="USA"
replace countrycode0=countrycode2 if countrycode=="USA" & countrycode0==""
replace countrycode0="USA" if tt==_N

foreach var in fst_distance_dominant nei_distance_dominant fst_distance_weighted nei_distance_weighted fst_stderr_dominant ///
			   nei_stderr_dominant fst_stderr_weighted nei_stderr_weighted gendist_fst_1500 gendist_nei_1500 fst_stderr_1500 nei_stderr_1500 {
		replace `var'=0 if tt==_N
}

foreach var in fst_stderr_dominant nei_stderr_dominant fst_stderr_weighted nei_stderr_weighted fst_stderr_1500 nei_stderr_1500 {
		replace `var'=1 if tt==_N
}


foreach var in fst_distance_dominant nei_distance_dominant fst_distance_weighted nei_distance_weighted fst_stderr_dominant ///
			   nei_stderr_dominant fst_stderr_weighted nei_stderr_weighted gendist_fst_1500 gendist_nei_1500 fst_stderr_1500 nei_stderr_1500 {
		rename `var' SP_US_`var'
}

keep SP_US* countrycode0
rename countrycode0 countrycode

keep countrycode SP_US_fst_distance_dominant SP_US_nei_distance_dominant
compress
save "`path0'Workfiles\distance_SP_US", replace



